/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.swing.calc;

import java.util.ArrayList;
import java.util.Stack;

/**
 *
 * @author Administrator
 */
public class CalcManger extends javax.swing.AbstractListModel{
    java.util.Stack<CalcLogic> calcStock;
    ArrayList<CalcLogic> inputList;
    double currentValue;
    private static double msValue;
    public CalcManger(){
       inputList=new ArrayList<CalcLogic>();
       calcStock=new Stack<CalcLogic>();
       msValue=0;
       reset();
    }

    public void addOP(AbstractOP opdata){
        CalcLogic prev=calcStock.pop();
        if(prev instanceof AbstractOP){
            calcStock.push(opdata);
        }else{
            calcStock.push(prev);
            calcStock.push(opdata);
        }
    }

    public void addCommand(AbstractCommand comm){
        CalcLogic prev=calcStock.pop();
        while(prev instanceof AbstractOP){
            prev=calcStock.pop();
        }
        if(prev instanceof DataOnly){
            currentValue=comm.calc(prev.calc(0));
            calcStock.push(new DataOnly(currentValue));
        }
        inputList.add(comm.clone());
        fireIntervalAdded(comm, inputList.size()-1,inputList.size()-1);
    }

    public void addData(double data){
        currentValue=data;
        if(!calcStock.isEmpty()){
           CalcLogic prev=calcStock.pop();
           if (prev instanceof AbstractOP){
               AbstractOP op=(AbstractOP)prev;
               op.setNum(data);
               CalcLogic theData=calcStock.pop();
                while(!(theData instanceof DataOnly)){
                   theData=calcStock.pop();
                }
               currentValue=op.calc(theData.calc(0));
               inputList.add(op.clone());
               fireIntervalAdded(op, inputList.size()-1,inputList.size()-1);
            }
        }
        if(data==currentValue){
           CalcLogic dat=new DataOnly(data);
           inputList.add(dat);
           fireIntervalAdded(dat, inputList.size()-1,inputList.size()-1);
        }
        calcStock.push(new DataOnly(currentValue));
    }

    public double getCurrentvalue(){
        return currentValue;
    }

    @Override
    public int getSize() {
        return inputList.size();
    }

    @Override
    public Object getElementAt(int index) {
        return inputList.get(index);
    }

    public void removeElementAt(int index){
        CalcLogic moveData=inputList.remove(index);
        retCalc();
        fireIntervalRemoved(moveData, index, index);
    }

    public String getLastOPName() {
        CalcLogic logic=calcStock.peek();
        String returnTxt="    ";
        if(logic instanceof AbstractOP){
            AbstractOP op=(AbstractOP)logic;
            returnTxt=" "+op.getOpStyle()+" ";
        }
        return returnTxt;
    }

    private void retCalc(){
    }

    public void reset(){
        if(inputList.size()>0)
           fireIntervalRemoved(this, 0, inputList.size()-1);
       inputList.clear();
       calcStock.clear();
       addData(0);

    }

        /**
     * @return the msValue
     */
    public static double getMsValue() {
        return msValue;
    }

    /**
     * @param aMsValue the msValue to set
     */
    public static void setMsValue(double aMsValue) {
        msValue = aMsValue;
    }
}
